package com.seek.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.seek.commonutils.ResponseResult;
import com.seek.eduservice.entity.EduComment;
import com.seek.eduservice.entity.EduCourseLike;
import com.seek.eduservice.service.EduCommentService;
import com.seek.eduservice.service.EduCourseLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: wml
 * @since: 2022/3/25 22:58
 */
@RestController
@RequestMapping("/eduservice/comment")
public class CommentController {

    @Autowired
    private EduCommentService eduCommentService;

    @Autowired
    private EduCourseLikeService eduCourseLikeService;

    // 根据课程 id 查看所有评论，根据添加时间排序
    @GetMapping(value = {"getCommentByCourseId/{courseId}/{memberId}/{current}/{size}", "getCommentByCourseId/{courseId}/{current}/{size}"})
    public ResponseResult getCommentByCourseId(@PathVariable String courseId,
                                               @PathVariable(required = false) String memberId,
                                               @PathVariable long current,
                                               @PathVariable long size) {
        long start = (current - 1) * size;
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        long total = eduCommentService.count(queryWrapper);
        List<EduCourseLike> courseLikeList = eduCommentService.getCommentByCourseId(courseId, memberId, start, size);
        return ResponseResult.ok().data("total", total).data("records", courseLikeList);
    }

    // 新增评论
    @PostMapping("addComment")
    public ResponseResult addComment(@RequestBody EduComment eduComment) {
        eduComment.setLikesCount(0);
        eduComment.setIsShow(1);
        eduCommentService.save(eduComment);
        EduCourseLike eduCourseLike = new EduCourseLike();
        eduCourseLike.setCourseId(eduComment.getCourseId());
        eduCourseLike.setMemberId(eduComment.getMemberId());
        eduCourseLike.setCommentId(eduComment.getId());
        eduCourseLikeService.save(eduCourseLike);
        return ResponseResult.ok();
    }

    // 根据评论 id 删除评论
    @DeleteMapping("removeCommentById/{id}")
    public ResponseResult removeCommentById(@PathVariable String id) {
        eduCommentService.removeById(id);
        QueryWrapper<EduCourseLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", id);
        eduCourseLikeService.remove(queryWrapper);
        return ResponseResult.ok();
    }

    // 根据评论 id，给点赞量加 1，并且根据评论 id，课程 id，用户 id设置 edu_course_like 表为已点赞
    @PutMapping("updateLikesCountById/{id}/{memberId}")
    public ResponseResult updateLikesCountById(@PathVariable String id, @PathVariable String memberId) {
        // 返回值
        String result = "";
        // 根据评论 id，查询评论 id，课程 id，用户 id，点赞量
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.select("id", "course_id", "member_id", "likes_count");
        EduComment eduComment = eduCommentService.getOne(queryWrapper);

        // 根据评论 id，课程 id，用户 id，查询是否点赞
        QueryWrapper<EduCourseLike> eduCourseLikeQueryWrapper = new QueryWrapper<>();
        eduCourseLikeQueryWrapper.eq("course_id", eduComment.getCourseId());
        eduCourseLikeQueryWrapper.eq("member_id", memberId);
        eduCourseLikeQueryWrapper.eq("comment_id", eduComment.getId());
        eduCourseLikeQueryWrapper.select("course_id", "member_id", "comment_id", "is_like");
        EduCourseLike eduCourseLike = eduCourseLikeService.getOne(eduCourseLikeQueryWrapper);
        // 点赞
        if (eduCourseLike == null) {
            eduCourseLike = new EduCourseLike();
            eduCourseLike.setCourseId(eduComment.getCourseId());
            eduCourseLike.setMemberId(memberId);
            eduCourseLike.setCommentId(eduComment.getId());
            eduCourseLike.setIsLike(1);
            eduCourseLikeService.save(eduCourseLike);
            eduComment.setLikesCount(eduComment.getLikesCount() + 1);
            result = "已点赞";
        } else {
            // 设置修改 Wrapper
            UpdateWrapper<EduCourseLike> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("course_id", eduComment.getCourseId());
            updateWrapper.eq("member_id", memberId);
            updateWrapper.eq("comment_id", eduComment.getId());
            // 取消点赞
            if (eduCourseLike.getIsLike() == 1) {
                updateWrapper.set("is_like", 0);
                eduComment.setLikesCount(eduComment.getLikesCount() - 1);
                result = "已取消点赞";
            } else { // 点赞
                updateWrapper.set("is_like", 1);
                eduComment.setLikesCount(eduComment.getLikesCount() + 1);
                result = "已点赞";
            }
            eduCourseLikeService.update(updateWrapper);
        }
        eduCommentService.updateById(eduComment);
        return ResponseResult.ok().data("result", result);
    }
}
